#!/usr/bin/env node

// 测试添加单词功能
const { addWordToLexicon, loadLexicon, saveLexicon } = require('./add-word.js');

async function testAddWord() {
  console.log('🧪 测试添加单词功能');
  console.log('='.repeat(40));
  
  // 测试数据
  const testWord = {
    word: 'testword',
    phonetic: '/test/',
    meaning: '测试单词',
    partOfSpeech: 'n.',
    difficulty: 1,
    frequency: 1,
    example: 'This is a test word.',
    exampleMeaning: '这是一个测试单词。',
    synonyms: ['test', 'example'],
    antonyms: ['real'],
    tags: ['测试', '临时']
  };
  
  try {
    // 显示添加前的统计
    console.log('📊 添加前统计:');
    const beforeWords = loadLexicon('cet4');
    console.log(`CET4 词库: ${beforeWords.length} 个单词`);
    
    // 添加测试单词
    console.log('\n➕ 添加测试单词...');
    const success = await addWordToLexicon('cet4', testWord);
    
    if (success) {
      // 显示添加后的统计
      console.log('\n📊 添加后统计:');
      const afterWords = loadLexicon('cet4');
      console.log(`CET4 词库: ${afterWords.length} 个单词`);
      
      // 验证单词是否真的被添加
      const addedWord = afterWords.find(w => w.word === 'testword');
      if (addedWord) {
        console.log('\n✅ 测试成功！单词已正确添加');
        console.log(`添加的单词: ${addedWord.word} - ${addedWord.meaning}`);
      } else {
        console.log('\n❌ 测试失败！单词未找到');
      }
    } else {
      console.log('\n❌ 测试失败！添加单词时出错');
    }
    
  } catch (error) {
    console.error('❌ 测试过程中发生错误:', error.message);
  }
}

// 运行测试
testAddWord();
